library(tidyverse)
library(readstata13)

## set working directory to Dataverse
## replication file
## Black and White broken into two separate files

###### WHITE RESPONDENTS ######################################
survey <- read.dta13("Harris_Data/Harris 1978 Attitudes Toward Racial and Religious Minorities and Toward Women, study no. S2829/harris_s2829_white_spss.dta")

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- as.character(2829)

# study year (year)
survey$year <- 1978

# geographic data (urban)
survey$urban <- as.character(survey$S13)
table(survey$urban)

# geographic data (region)
survey$region <- as.character(survey$S11)
table(survey$region)

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
survey$inequality <- as.character(survey$Q13_2)
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
survey$union.self <- as.character(survey$F5_1)
survey$union.self[survey$F5_4 == "Yes"] <- "Not Sure"

survey$union.other <- as.character(survey$F5_2)
survey$union.other[survey$F5_4 == "Yes"] <- "Not Sure"

table(survey$union.self)
table(survey$union.other)

# employment (employed)
survey$employed <- as.character(survey$F1A)
table(survey$employed)

# empl self
survey$employed.self <- NA

# occupation
survey$occupation <- as.character(survey$F1B)
table(survey$occupation)

## occ self
survey$occupation.self <- NA 

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
survey$educ <- as.character(survey$F4)
table(survey$educ)                       

# household income (income)
survey$income <- as.character(survey$F6)
table(survey$income)

# age
survey$age <- as.character(survey$F3)
table(survey$age)

# race
survey$race <- as.character(survey$F7)
table(survey$race)

# politics (party)
survey$party <- as.character(survey$Q24C)
table(survey$party)

# politics (ideology)
survey$ideology <- NA

# gender
survey$gender <- as.character(survey$F8)
table(survey$gender)

# religion
survey$religion <- NA

# factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- as.character(survey$Q13_1)
survey$dontcount <- as.character(survey$Q13_3)
survey$leftout <- as.character(survey$Q13_4)

## quesiton placement
survey$question_place <- "before party"

# subset
survey_2829white <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                              "inequality", "inequality.variable", "union.self", "union.other",
                              "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                              "age", "race", "party", "ideology", "gender", "religion",
                              "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                              "question_place")]

###################################################################################
# BLACK RESPONDENTS
#####################################################################33

survey <- read.dta13("Harris_Data/Harris 1978 Attitudes Toward Racial and Religious Minorities and Toward Women, study no. S2829/harris_s2829_black_spss.dta")

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- as.character(2829)

# study year (year)
survey$year <- 1978

# geographic data (urban)
survey$urban <- NA

# geographic data (region)
survey$region <- NA

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
survey$inequality <- as.character(survey$Q13_2)
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
survey$union.self <- as.character(survey$F5_1)
survey$union.self[survey$F5_4 == "Yes"] <- "Not Sure"

survey$union.other <- as.character(survey$F5_2)
survey$union.other[survey$F5_4 == "Yes"] <- "Not Sure"

table(survey$union.self)
table(survey$union.other)

# employment (employed)
survey$employed <- as.character(survey$F1A)
table(survey$employed)

# empl self
survey$employed.self <- NA

# occupation
survey$occupation <- as.character(survey$F1B)
table(survey$occupation)

## occ self
survey$occupation.self <- NA 

# household size (hhsize)
survey$hhsize <- NA

# education (educ)
survey$educ <- as.character(survey$F4)
table(survey$educ)                       

# household income (income)
survey$income <- as.character(survey$F6)
table(survey$income)

# age
survey$age <- as.character(survey$F3)
table(survey$age)

# race
survey$race <- as.character(survey$F7)
table(survey$race)

# politics (party)
survey$party <- as.character(survey$Q24C)
table(survey$party)

# politics (ideology)
survey$ideology <- NA

# gender
survey$gender <- as.character(survey$F8)
table(survey$gender)

# religion
survey$religion <- NA

# factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- as.character(survey$Q13_1)
survey$dontcount <- as.character(survey$Q13_3)
survey$leftout <- as.character(survey$Q13_4)

## quesiton placement
survey$question_place <- "before party"

# subset
survey_2829black <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                              "inequality", "inequality.variable", "union.self", "union.other",
                              "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                              "age", "race", "party", "ideology", "gender", "religion",
                              "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                              "question_place")]

## rbind
survey_S2829 <- rbind(survey_2829white, survey_2829black)


survey_S2829$pid <- c(1:nrow(survey_S2829))


#saveRDS(survey_S2829, file = "Harris_Data/survey_S2829.rds")


